package com.ehualu.javahadoop.until;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: 吴敬超
 * @Date: 2020/3/3 15:35
 */
public class Timeuntil {


    /*
     * 根据起始时间，得到日期的集合
     * */
    public List<String> getDays(String startTime, String endTime) {

        // 返回的日期集合
        List<String> days = new ArrayList<String>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");

//        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(simpleDateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return days;
    }


    /*
     *
     * 得到 hive里需要的日期时间格式
     * 例：'20200226','20200227','20200228'
     * */
    public String getDaysstr(List<String> listdays) {

        String str = "";
        Integer listsiz = listdays.size();

        for (int i = 0; i < listsiz; i++) {
            String stradd = "'" + listdays.get(i) + "'";

            str = str + stradd;

            if (i < listsiz - 1) {

                str = str + ",";
            }
        }

        return str;
    }


    /*
     *判断endtime 对应的其日期是否比当前早
     *
     * */
    public boolean timeflag(String endtime) throws ParseException {

        boolean flag = false;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        String endTimeString = endtime.split(" ")[0];

        String nowtimeString = sdf.format(new Date());
        Date endTimeDate = sdf.parse(endTimeString);
        Date nowtimeTimeDate = sdf.parse(nowtimeString);

        if (endTimeDate.before(nowtimeTimeDate)) {

            System.out.println("end< now");

            flag = true;
        } else {

            System.out.println("end 不小于 now");

//            flag = false;
        }

        return flag;
    }
}
